
package com.zdx.yswl.ui.base;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.Toast;

import com.zdx.yswl.App;
import com.zdx.yswl.R;
import com.zdx.yswl.base.KActivity;
import com.zdx.yswl.tool.IHandlerLikeNotify;
import com.zdx.yswl.tool.IHandlerNotify;


/**
 * 和设备有关的抽象的活动基类，所有活动都应该继承这个类，并实现其抽象方法和接口
 * 已经合并到BaseActivity中，该类没有使用
 */
public abstract class DevBaseActivity extends KActivity implements IHandlerNotify, IHandlerLikeNotify
{
    /** 初始化设置，不要在这里写费时的操作 */
    protected abstract void initSettings();

    /** 初始化界面，不要在这里写费时的操作 */
    protected abstract void initUi();

    /** 保存设置，不要在这里写费时的操作 */
    protected abstract void saveSettings();

    /** 释放资源、解锁、删除不用的对象，不要在这里写费时的操作 */
    protected abstract void freeMe();

    protected ProgressDialog proDialog;
    protected Toast toast;

    private IHandlerNotify notify = this;
    protected MyHandler handler = new MyHandler(this);

    protected static class MyHandler extends Handler
    {

        private DevBaseActivity activity;

        public MyHandler(DevBaseActivity activity)
        {
            this.activity = activity;
        }

        @Override
        public void handleMessage(Message msg)
        {
            activity.notify.onHandler(msg.what, msg.arg1, msg.arg2, msg.obj);
            super.handleMessage(msg);
        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // ((App) getApplication()).push(this);
        ((App) getApplication()).setCurrentNotifyer(this);
        initSettings();
        initUi();
    }

    @Override
    protected void onDestroy()
    {
        // ((App) getApplication()).pop();
        freeMe();
        super.onDestroy();
    }

    @Override
    protected void onPause()
    {
        saveSettings();
        super.onPause();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    /**
     * 弹dialog
     * @param msg
     */
    public void createDialog(String msg, boolean cancel)
    {

        if (null == msg || "".equalsIgnoreCase(msg))
        {
            msg = getResources().getString(R.string.waiting);
        }
        try
        {
            if (null != DevBaseActivity.this && !DevBaseActivity.this.isFinishing())
            {
                if (null == proDialog)
                {
                    proDialog = new ProgressDialog(DevBaseActivity.this);
                }
                proDialog.setMessage(msg);
                if (null != proDialog)
                {
                    if (null != DevBaseActivity.this
                            && !DevBaseActivity.this.isFinishing())
                    {
                        proDialog.show();
                        proDialog.setCancelable(cancel);
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        proDialog.setOnKeyListener(onKeyListener);
    }

    /**
     * progressDialog返回键监听
     */
    private DialogInterface.OnKeyListener onKeyListener = new DialogInterface.OnKeyListener()
    {
        @Override
        public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent)
        {
            if(i == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_DOWN)
            {
                dismissDialog();
            }
            return false;
        }
    };

    /**
     * 关闭dialog
     */
    public void dismissDialog()
    {
        if (null != proDialog)
        {
            proDialog.dismiss();
            proDialog = null;
        }
    }

    /**
     * 弹系统消息
     */
    public void showTextToast(int id)
    {
        String msg = getApplication().getResources().getString(id);
        if (toast == null) {
            toast = Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }

    /**
     * 弹系统消息
     * @param msg
     */
    public void showTextToast(String msg)
    {
        if (toast == null) {
            toast = Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT);
        } else
        {
            toast.setText(msg);
        }
        toast.show();
    }
}
